package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.InventoryListBean;
import com.example.sunmiv1.listener.ClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class InventoryAdapter extends BaseAdapter {

    private Context context;
    private List<InventoryListBean.DataBean> list;
    ViewHolder viewHolder;
    private Joggle joggle;
    private int type = 0;
    private ClickListener mClickListener;

    public InventoryAdapter(Context context, List<InventoryListBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setClickListener(ClickListener clickListener) {
        mClickListener = clickListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.inventory_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Resources resources = context.getResources();
        viewHolder.inventoryStatus.setText(resources.getString(R.string.deposit_status) + ":" + list.get(position).getStatus_name());
        viewHolder.inventoryAdapterPeople.setText(resources.getString(R.string.inventoryList_people) + list.get(position).getOperator());
        viewHolder.inventoryAdapterTime.setText(resources.getString(R.string.inventoryList_time) +
                list.get(position).getAdd_time());
        viewHolder.inventoryAdapterOrder.setText(resources.getString(R.string.inventoryList_order) + list.get(position).getOrder_number());
        viewHolder.inventoryAdapterMoney.setText(resources.getString(R.string.inventoryList_money) +
                list.get(position).getAmount());
        viewHolder.inventoryAdapterNum.setText(resources.getString(R.string.inventoryList_num) + list.get(position).getQuantity());
        if ("1".equals(list.get(position).getStatus())) {
            viewHolder.continueInventoryInfo.setText(R.string.inventoryTitle);
        } else {
            viewHolder.continueInventoryInfo.setText(R.string.inventoryTitle1);
        }
        final int index = position;
        viewHolder.continueInventoryInfo.setOnClickListener(v -> {
            mClickListener.clickListener(v, index,1);
        });
        viewHolder.inventoryDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.clickListener(v, index,2);
            }
        });
        return convertView;
    }

    public interface Joggle {
        void onclick(int position);
    }

    public void Onclick(Joggle joggle) {
        this.joggle = joggle;
    }

    static class ViewHolder {
        @BindView(R.id.inventory_status)
        TextView inventoryStatus;
        @BindView(R.id.inventory_adapter_people)
        TextView inventoryAdapterPeople;
        @BindView(R.id.inventory_adapter_time)
        TextView inventoryAdapterTime;
        @BindView(R.id.inventory_adapter_order)
        TextView inventoryAdapterOrder;
        @BindView(R.id.inventory_adapter_money)
        TextView inventoryAdapterMoney;
        @BindView(R.id.inventory_adapter_num)
        TextView inventoryAdapterNum;
        @BindView(R.id.view_inventoryInfo)
        View viewInventoryInfo;
        @BindView(R.id.continue_inventoryInfo)
        TextView continueInventoryInfo;
        @BindView(R.id.inventory_detail)
        TextView inventoryDetail;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
